# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import json
import logging
import time

import requests
from itemadapter import ItemAdapter
from shiGuang.items import ShiguangTypeItem


class ShiguangTypeIdPipeline:
    # 处理时光盒子的案例视频上传
    typeId = {}

    def process_item(self, item, spider):
        """
        :title: 处理上传类型和案例
        :param item:
        :param spider:
        :return:
        """
        if isinstance(item, ShiguangTypeItem):  # 处理案例类型数据
            req = requests.post(url='www.xxx.com',  # 上传类型接口
                                data=json.dumps(dict(item)),
                                headers={
                                    'Content-Type': 'application/json'
                                })
            if req.status_code == 200:
                self.typeId = eval(req.text)['data']
                print('typeId 类型存储完成---{}'.format(self.typeId))
            else:
                print('类型接口返回失败---{}'.format(item))
        else:  # 处理案例详情页数据
            # print(self.typeId[item['page']['dic_series_category']['id']])
            item['page']['typeId'] = self.typeId[item['page']['dic_series_category']['id']]
            req = requests.post(url='www.xxx.com',  # 上传案例接口
                                data=json.dumps(dict(item)),
                                headers={
                                    'Content-Type': 'application/json'
                                })
            if req.status_code == 200:
                print('案例存储完成---{}'.format(item['page']['title']))
            else:
                print('案例存储失败---{}'.format(item['page']['title']))


class LuoShanPipeline:
    # xxx系统的案例视频图片上传

    def process_item(self, item, spider):
        """
        :title: 处理上传类型和案例
        :param item:
        :param spider:
        :return:
        """
        req = requests.post(url='http://xxx.com',  # 上传案例接口
                            data=json.dumps(dict(item)),
                            headers={
                                'Content-Type': 'application/json',
                                'Tenant-Id': 161028
                            })
        print(item)
        if req.status_code == 200:
            print('案例存储完成')
        else:
            print('案例存储失败')


class wkPipeline:
    # xxx系统的客资导入

    def __init__(self):
        self.a = 0  # 成功计数
        self.b = 0  # 失败计数
        self.mistake_list = []  # 失败数组
        self.success_list = []  # 成功数组

    def open_spider(self, spider):
        print('请求开始')

    def process_item(self, item, spider):
        """
        :title: 处理上传类型和案例
        :param item:
        :param spider:
        :return: http://xxx.com
        'Blade-Auth'    需要更换
        """
        req = requests.post(url='http://xxx.com',  # 上传接口
                            data=json.dumps(dict(item)),
                            headers={
                                'Content-Type': 'application/json',
                                'Blade-Auth': 'bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'
                                              '.eyJ0ZW5hbnRfaWQiOiIzMjIzMDciLCJvcGVuX2lkIjoiIiwidXNlcl9uYW1lIjoiMjU1NDMxMzk4OTkiLCJyZWFsX25hbWUiOiIiLCJjbGllbnRfaWQiOiJhcHBjbGllbnQiLCJyb2xlX2lkIjoiMTEyMzU5ODgxNjczODY3NTIwNCwxMTIzNTk4ODE2NzM4Njc1MjAzLDExMjM1OTg4MTY3Mzg2NzUyMDYsMTU1NDM0NTI4Njg4NTIzNjczNywxNTU0MzQ1MjYzMTI0NTA0NTc3LDE1NTQzNDUzMDM2MzMwOTI2MTAiLCJzY29wZSI6WyJhbGwiXSwidW5pb25faWQiOiIiLCJvYXV0aF9pZCI6IiIsInN0b3JlTmFtZSI6IuWWnOWggiIsImV4cCI6MTY2NzgwMTkyNywianRpIjoiZjY4Y2Q1OTItOTMxZi00MzQ5LTg5NTQtYWE0NTY2M2EwMzI1Iiwic3RvcmVfaWQiOjE1MDgzMjQ4MDYzMDYwMTcyODEsImJyYW5kTmFtZSI6IuWWnOWggu-8iOS4reWbve-8ieWbvemZheWpmuekvOWggiIsImxvZ29QYXRoIjoiaHR0cHM6Ly9uYW5jb28ub3NzLm5hbmNvby5jbi91cGxvYWQvMjAyMjAzMjgvYTIyZmE3OWM1OTJhYTU4NWM2ZDdiMmVjNzU5Nzg4MWIucG5nIiwiYXZhdGFyIjoiIiwic3RvcmVJZCI6MTUwODMyNDgwNjMwNjAxNzI4MSwiYXV0aG9yaXRpZXMiOlsiZ2VuZXJhbCIsImhpZ2giLCJtaWRkbGUiXSwicm9sZV9uYW1lIjoibWlkZGxlLGhpZ2gsZ2VuZXJhbCxoaWdoLG1pZGRsZSxnZW5lcmFsIiwibGljZW5zZSI6InBvd2VyZWQgYnkgbmFuY29vIiwicG9zdF9pZCI6IjE1MDgzMjY1ODUzNDUyMjQ3MjgiLCJ1c2VyX2lkIjoiMTUxMzUxMTA2NjU5MDk0MTE4NiIsIm5pY2tfbmFtZSI6IuWtmeeOriIsImRldGFpbCI6eyJ0eXBlIjoid2ViIn0sImRlcHRfaWQiOiIxNTA4MzI2NTg1MDEzODc0Njg5IiwiYWNjb3VudCI6IjI1NTQzMTM5ODk5IiwiYXBwbGV0c19vcGVuX2lkIjoiIn0.BWgyMqBmRufHLiQwjTW4ghZGVqKO8s4rADBW8nJqRK8',
                                'Authorization': 'Basic YXBwY2xpZW50OmFwcGNsaWVudDEyMzQ=',
                                'Tenant-Id': '322307'
                            })
        if req.status_code == 200:
            self.success_list.append(item['page']['contract_number'])  # 存到成功的数组里面
            self.a = self.a + 1
            print('{}存储成功，第{}条数据,合同编号:{}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), self.a,
                                                 item['page']['contract_number']))
            logging.warning(item['page'])  # 存日志
            return '客资存储成功'
        else:
            self.mistake_list.append(item['page']['contract_number'])  # 存到失败的数组里面
            self.b = self.b + 1
            print('{}存储失败，第{}条数据,合同编号:{}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), self.b,
                                                 item['page']['contract_number']))
            logging.warning(item['page'])   # 存日志
            return '客资存储失败'

    def close_spider(self, spider):
        print('存储结束,共进入管道{}条'.format(self.a + self.b))
        logging.warning('失败数据')
        logging.warning(self.mistake_list)
        logging.warning(list(set(self.mistake_list)))
        logging.warning('成功数据')
        logging.warning(self.success_list)
        logging.warning(list(set(self.success_list)))
